package com.ttloner1.exercise;

import com.ttloner1.bean.ExamStudent;
import com.ttloner1.util.JDBCUtils;
import org.junit.Test;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author TTLoner
 * @date 2021/6/22 15:34
 * @description
 * 练习1：从控制台向数据库的表customers插入一条数据
 * Utils类已经定义好，但是为了增加印象决定自己手写一个写死的
 */
public class Exercise1 {
//    public static void main(String[] args) throws ClassNotFoundException, SQLException, ParseException, IOException {
//        Scanner scanner = new Scanner(System.in);
//        InputStream stream =ClassLoader.getSystemResourceAsStream("jdbc.properties");
//        Properties properties = new Properties();
//        properties.load(stream);
//        String user = properties.getProperty("user");
//        String password = properties.getProperty("password");
//        String url = properties.getProperty("url");
//        String driverClass = properties.getProperty("driverClass");
//        //加载驱动
//        Class.forName(driverClass);
//        //简历连接
//        Connection conn = DriverManager.getConnection(url, user, password);
//        String sql ="insert into customers(name,email,birth) values(?,?,?)";
//        PreparedStatement ps = conn.prepareStatement(sql);
//        System.out.println("输入第一个参数");
//        ps.setObject(1,scanner.next());
//        System.out.println("输入第二个参数");
//        ps.setObject(2,scanner.next());
//        System.out.println("输入第三个参数");
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date date = sdf.parse(scanner.next());
//        System.out.println(date);
//        ps.setObject(3, new java.sql.Date(date.getTime()));
//        ps.execute();
//        ps.close();
//        conn.close();
//    }
    /**
     * @author: TTLoner
     * @time: 2021/6/22 17:26
     * @description:
     *第二题，向表中添加数据
     */

    @Test
    public void test2(){
        Connection conn = JDBCUtils.getConnection();
        String sql="insert into examstudent(FlowID,Type,IDCard,ExamCard,StudentName,Location,Grade)" +
                "values(?,?,?,?,?,?,?)";
        String[] args={"7","4","412824195263214584","200523164754000", "张锋","郑州","85"};
        JDBCUtils.update(conn,sql,args);
    }

    /**
     * @author: TTLoner
     * @time: 2021/6/22 19:39
     * @description:
     *  输入身份证号和准考证号查询学生信息
     */
    public static void main(String[] args) throws InstantiationException {
        Connection conn = JDBCUtils.getConnection();
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择输入的类型");
        System.out.println("a:身份证号");
        System.out.println("b:准考证号");
        System.out.println("c：删除，请输入准考证号");
        System.out.println("按q退出");
        String choic = sc.next();
        if(choic.equals("a")){
            String sql="select * from examstudent where IDCard=?";
            String[] obj={sc.next()};
            ArrayList<ExamStudent> instance = JDBCUtils.getInstance(conn,ExamStudent.class, sql, obj);
            System.out.println(instance);
        }else if(choic.equals("b")){
            String sql="select * from examstudent where ExamCard=?";
            String[] obj={sc.next()};
            ArrayList<ExamStudent> instance = JDBCUtils.getInstance(conn,ExamStudent.class, sql, obj);
            System.out.println(instance);
        }else if (choic.equals("c")){
            String sql="delete from examstudent where ExamCard=?";
            String[] obj={sc.next()};
            int i = JDBCUtils.update(conn,sql, obj);
            if(i==0) System.out.println("查无此人");
            else System.out.println("删除成功");
        }
        else System.out.println("你必须输入a,b,c之一");
        JDBCUtils.closeConnection(conn,null);
    }
}
